/***     
 * 找对子工具类
 */

var _ = require('../Underscore');
var FindSameCardValueTool = require('./FindSameCardValueTool');
var CardTool = require('./../CardTool');
var FindPairsTool = module.exports = {};

/***
 * _pairsNumber:要找的对子数量
 */
FindPairsTool.getResult = function(_cardInfoList, _pairsNumber) {
    var result = this.check(_cardInfoList, _pairsNumber);
    // CardTool.LogResult(result, "找对子");
    return result;
};

FindPairsTool.check = function(_cardInfoList, _pairsNumber) {
    var cardGroupList = FindSameCardValueTool.check(_cardInfoList);
    var result = {
        isFind: false,
        otherGroup: [],
    }

    var otherGroup = []; //无法组成对的牌
    var canPaitGroup = []; //可以组成对的牌
    _.each(cardGroupList, function(_sameCardGroup) {
        var size = _.size(_sameCardGroup);
        if (size == 1) {
            otherGroup.push(_sameCardGroup);
        } else {
            result.isFind = true;
            canPaitGroup.push(_sameCardGroup);
        }
    });

    //存储一对一对
    var singlePairsList = [];

    _.each(canPaitGroup, function(_sameCardGroup) {
        var size = _.size(_sameCardGroup);
        if (2 == size) {
            singlePairsList.push(_sameCardGroup);
        } else if (3 == size) {
            var tmpGroupList = _.partition(_sameCardGroup, function(info, idx) {
                return idx >= 1;
            })
            singlePairsList.push(tmpGroupList[0]);
            otherGroup.push(tmpGroupList[1]);
        } else if (4 == size) {
            var tmpGroupList = _.partition(_sameCardGroup, function(info, idx) {
                return idx >= 1;
            })
            singlePairsList.push(tmpGroupList[0]);
            singlePairsList.push(tmpGroupList[1]);
        } else if (5 == size) {
            var tmpGroupList = _.partition(_sameCardGroup, function(info, idx) {
                return idx >= 1;
            })
            singlePairsList.push(tmpGroupList[0]);

            tmpGroupList = _.partition(tmpGroupList[1], function(info, idx) {
                return idx >= 1;
            })
            singlePairsList.push(tmpGroupList[0]);
            otherGroup.push(tmpGroupList[1]);
        }
    })

    otherGroup = _.flatten(otherGroup);
    result.otherGroup = otherGroup;
    result.findGroup = singlePairsList;
    // console.error("无法组成对的牌", JSON.stringify(otherGroup));
    // console.error("可以组成对的牌", JSON.stringify(singlePairsList));
    //分离能成对的牌
    return result;
};